import { Camera, Color, director, geometry, GeometryRenderer, Mat4, Vec3, VERSION } from 'cc';
// 方法1：通过pipeline获取

export class Debug {

    static renderer: GeometryRenderer = null;
    static camera: Camera = null;


    static getRenderer(): GeometryRenderer {

        if (!this.renderer) {
            let num = parseInt(VERSION.charAt(2));
            if (num < 6) { // < 3.6 ver
                this.renderer = director!.root!.pipeline.geometryRenderer;
            } else { // >= 3.6 ver
                if (this.camera)
                    this.camera.camera['initGeometryRenderer']();
                this.renderer = this.camera.camera["geometryRenderer"];
                //director.root.scenes[0].cameras[0]["geometryRenderer"];
            }
            //             const pipeline = director.root.pipeline;
            // const commandBuffer = pipeline.commandBuffers[0]; // 主命令缓冲区
            // console.log("commandBuffer", commandBuffer)
            // commandBuffer.setLineWidth(10)

        }

        return this.renderer;
    }
    static addAABB(aabb: geometry.AABB, color: Color) {
        let renderer = this.getRenderer();
        renderer.addBoundingBox(aabb, color, true, true, true, true, Mat4.IDENTITY);
    }

    static addBox(bound: Array<number>, color: Color) {

        let renderer = this.getRenderer();

        let cx = bound[0] + bound[3], cy = bound[1] + bound[4], cz = bound[2] + bound[5];
        let lx = bound[3] - bound[0], ly = bound[4] - bound[1], lz = bound[5] - bound[2];
        let aabb = geometry.AABB.create(cx * 0.5, cy * 0.5, cz * 0.5, lx * 0.5, ly * 0.5, lz * 0.5);
        renderer.addBoundingBox(aabb, color, true, true, true, true, Mat4.IDENTITY);
    }

    static addTriangle(tris: Array<number>, world: Mat4, color: Color) {

        let renderer = this.getRenderer();
        if (renderer && renderer.addBoundingBox) {

            let v0: any = new Vec3(tris[0], tris[1], tris[2]).transformMat4(world);
            let v1: any = new Vec3(tris[3], tris[4], tris[5]).transformMat4(world);
            let v2: any = new Vec3(tris[6], tris[7], tris[8]).transformMat4(world);

            renderer.addTriangle(v0, v1, v2, color);
        }

    }


    static addLine(v0: Vec3, v1: Vec3, world: Mat4, color: Color) {

        let renderer = this.getRenderer();
        if (renderer && renderer.addLine) {
            renderer.addLine(v0, v1, color);
        }

    }
}

